unit GrammarDebugger;

interface

uses
  Windows, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, PasFileParser, FormalGrammars, PascalFormalGrammars, ComCtrls,
  GrammarTree, StdCtrls, ExtCtrls, Buttons, Spin, Messages, ToolWin;

type
  TLexemeDebuggerForm = class(TForm)
    GrammarTreeView1: TGrammarTreeView;
    RichEdit1: TRichEdit;
    ListView1: TListView;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    procedure FormCreate(Sender: TObject);
    procedure GrammarTreeView1CustomDrawItem(Sender: TCustomTreeView; Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
  strict private
    fLexemeBreakPoints: TList;
    fLexeme: ILexeme;
    procedure OnHotKey(var m: TMessage); message WM_HOTKEY;
    procedure SetLexeme(const Value: ILexeme);
  protected
    procedure StepIn;
    procedure Step;
    procedure StepOut;
  published
  public
    class procedure Debug(const l: ILexeme; const target: string);
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property TargetLexeme: ILexeme read fLexeme write SetLexeme;
  end;

implementation

{$R *.dfm}

const
  HK_STEP_IN = 1;
  HK_STEP = 2;
  HK_STEP_OUT = 3;

constructor TLexemeDebuggerForm.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  fLexemeBreakPoints := TList.Create;
end;

class procedure TLexemeDebuggerForm.Debug(const l: ILexeme; const target: string);
var
  f: TLexemeDebuggerForm;
begin
  Assert(Assigned(l), 'Lexeme must be assigned');
  f := TLexemeDebuggerForm.Create(nil);
  try
    f.TargetLexeme := l;
    f.ShowModal;
  finally
    f.Free;
  end;
end;

destructor TLexemeDebuggerForm.Destroy;
begin
  FreeAndNil(fLexemeBreakPoints);
  inherited;
end;

procedure TLexemeDebuggerForm.FormCreate(Sender: TObject);
begin
//  if not RegisterHotKey(Handle, HK_STEP, 0, VK_F8) then
//    RaiseLastOSError;
//  if not RegisterHotKey(Handle, HK_STEP_IN, 0, VK_F7) then
//    RaiseLastOSError;
//  if not RegisterHotKey(Handle, HK_STEP_OUT, MOD_SHIFT, VK_F8) then
//    RaiseLastOSError;
end;

procedure TLexemeDebuggerForm.GrammarTreeView1CustomDrawItem(Sender: TCustomTreeView; Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
//
end;

procedure TLexemeDebuggerForm.OnHotKey(var m: TMessage);
begin
  case m.WParam of
    HK_STEP_IN:
      StepIn;

    HK_STEP:
      Step;

    HK_STEP_OUT:
      StepOut;

    else
      raise Exception.Create('Not implemented');
    
  end;
end;

procedure TLexemeDebuggerForm.SetLexeme(const Value: ILexeme);
begin
  if fLexeme <> Value then
  begin
    fLexeme := Value;
    GrammarTreeView1.Lexeme := Value;
  end;
end;

procedure TLexemeDebuggerForm.Step;
begin

end;

procedure TLexemeDebuggerForm.StepIn;
begin

end;

procedure TLexemeDebuggerForm.StepOut;
begin

end;

end.
